<?php

namespace App\Http\Controllers\admin\system;

use App\Http\Controllers\Controller;
use App\Models\admin\AdminRoleModel;
use App\Models\admin\AdminUserModel;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use lxs\api\ApiResponse;

class RoleController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $params = $request->all();
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;
        $offset = ($page - 1) * $limit;
        $model = AdminRoleModel::query();
        if (!empty($params['keyword'])) {
            $model->where('role_name', 'like', '%' . $params['keyword'] . '%');
        }
        $total = $model->count();
        $data = $model->offset($offset)->limit($limit)->get();
        return ApiResponse::success([
            'data'  => $data,
            'total' => $total
        ]);
    }

    public function select(Request $request): JsonResponse
    {
        $data = AdminRoleModel::select(['id', 'role_name'])->get()->toArray();
        return ApiResponse::success($data);
    }

    public function read(Request $request): JsonResponse
    {
        $id = $request->input('id');
        $role = AdminRoleModel::findOrFail($id);
        return ApiResponse::success($role);
    }

    public function add(Request $request): JsonResponse
    {
        $data = $request->validate([
            'role_name'   => 'required|string',
            'permissions' => 'array',
            'remark'      => 'string'
        ]);
        if (AdminRoleModel::where('role_name', $data['role_name'])->exists()) {
            return ApiResponse::error('角色名已存在');
        }
        $role = AdminRoleModel::create([
            'role_name'   => $data['role_name'],
            'permissions' => $data['permissions'],
            'remark'      => $data['remark']
        ]);
        if (!$role) {
            return ApiResponse::error('添加失败');
        }
        return ApiResponse::success('添加成功');
    }

    public function update(Request $request): JsonResponse
    {
        $data = $request->validate([
            'role_name'   => 'required|string',
            'permissions' => 'array',
            'remark'      => 'string'
        ]);
        $id = $request->input('id');
        //查看是否重复
        if (AdminRoleModel::where('role_name', $data['role_name'])->where('id', '!=', $id)->exists()) {
            return ApiResponse::error('角色名已存在');
        }
        $role = AdminRoleModel::findOrFail($id);
        if ($role->is_super == 1) {
            return ApiResponse::error('超级管理员不允许修改');
        }
        $role->role_name = $data['role_name'];
        $role->permissions = $data['permissions'];
        $role->remark = $data['remark'];
        if (!$role->save()) {
            return ApiResponse::error('修改失败');
        }
        return ApiResponse::success('修改成功');
    }

    public function editPermission(Request $request): JsonResponse
    {
        $data = $request->validate([
            'permissions' => 'required|array',
        ]);
        $id = $request->input('id');
        $role = AdminRoleModel::findOrFail($id);
        if ($role->is_super == 1) {
            return ApiResponse::error('超级管理员不允许修改');
        }

        $role->permissions = $data['permissions'];
        if (!$role->save()) {
            return ApiResponse::error('修改失败');
        }
        return ApiResponse::success('修改成功');
    }

    public function delete(Request $request): JsonResponse
    {
        $id = $request->input('id');
        $role = AdminRoleModel::findOrFail($id);
        if ($role->is_super == 1) {
            return ApiResponse::error('超级管理员不允许删除');
        }
        //判断是否有用户使用该角色
        if (AdminUserModel::where('role_id', $id)->exists()) {
            return ApiResponse::error('有用户使用该角色');
        }
        if (!$role->delete()) {
            return ApiResponse::error('删除失败');
        }
        return ApiResponse::success('删除成功');
    }
}
